package com.netbao.fin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.netbao.fin.mapper.SubtotalMapper;
import com.netbao.fin.mapper.TotalMapper;
import com.netbao.fin.model.Subtotal;
import com.netbao.fin.model.Total;
import com.netbao.fin.service.ShenService;
import com.netbao.fin.vomapper.ShenVoMapper;
import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.sys.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author suqk
 * @create 2019-11-03 15:38
 */
@Service
public class ShenServiceImpl implements ShenService {
    @Resource
    private ShenVoMapper shenVoMapper;
    @Resource
    private SubtotalMapper subtotalMapper;
    @Resource
    private TotalMapper totalMapper;
    @Override
    public PageResult listAll(PageBean pageBean, String name) {
        Integer eid= ShiroUtils.getUserEntity().getEid();
        if(!StringUtils.isEmpty(name)){
            PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
            PageInfo<Total> userPageInfo = new PageInfo<Total>(shenVoMapper.mo(pageBean,name,(long)eid));
            return new PageResult(userPageInfo.getTotal(),userPageInfo.getList());
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        PageInfo<Total> userPageInfo = new PageInfo<Total>(shenVoMapper.listAll(pageBean,(long)eid));
        return new PageResult(userPageInfo.getTotal(),userPageInfo.getList());
    }


    @Override
    public Result saveSubtotal(Total total) throws ParseException {
        Subtotal subtotal=new Subtotal();
        subtotal.setCstatus((long)2);
        subtotal.setId(total.getOpid());
        subtotal.setCreason(total.getCreason());
        Date date=null;
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        date=dateFormat.parse(dateFormat.format(new Date()));
        subtotal.setShentime(date);
        subtotalMapper.updateByPrimaryKeySelective(subtotal);
        shenVoMapper.exit(total.getId());
        return Result.ok();
    }

    @Override
    public Result tong(Long id, Long opid) throws ParseException {
        totalMapper.deleteByPrimaryKey(id);
        Subtotal subtotal=new Subtotal();
        subtotal.setId(opid);
        subtotal.setCstatus((long)3);
        subtotal.setPaystatus((long)1);
        Date date=null;
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        date=dateFormat.parse(dateFormat.format(new Date()));
        subtotal.setShentime(date);
        subtotalMapper.updateByPrimaryKeySelective(subtotal);
        return Result.ok();
    }
}
